#include "Menu_Ctrl.h"

/*文件内部使用的宏*/

/*静态全局变量*/


/*全局变量*/
volatile uint8_t machine_rest_flag = MACHINE_OPEN;//机器状态 MACHINE_OPEN--开机 MACHINE_CLOSE--关机
extern volatile bool nightled_flag;//WS2812B灯带LED呼吸灯打开标志
extern volatile uint8_t led_switch;//控制LED灯带开关
extern volatile uint8_t led_ctrl_idx;//控制WS2812B的色彩
extern volatile uint8_t WS2812B_brightness;//控制WS2812B的亮度
extern volatile uint8_t water_switch;//雾化器开关标志 0--关闭 1--开启

void menu_proc(uint8_t event)
{
    static uint8_t led_temp = 0;//按键1长按关闭或开启WS2812B
    
    
    switch(event)
    {
    case MENU_EVENT_TICK:
    {
        if(machine_rest_flag != MACHINE_CLOSE)
        {

        }

        break;
    }
    case MENU_EVENT_BUTTON1:
    {
        // if(led_switch == false)
        // {
        //     led_switch = true;
        //     //Led_Open();
        //     WS2812B_Open();
        // }
        // else 
        // {
        //     led_switch = false;
        //     //Led_Close();
        //     WS2812B_Close();
        // }
        if(machine_rest_flag != MACHINE_CLOSE)
        {
            nightled_flag = false;//关呼吸灯

            //WS2812B_Close();

            led_ctrl_idx++;
            if(led_ctrl_idx > 7)
            {
                led_ctrl_idx = 0;
            }
            
            led_temp = 0;

            Update_WS2812B_Brightness_To_Aliyun(WS2812B_brightness);

            switch(led_ctrl_idx)
            {
            case 0:
            {
                WS2812B_SetGRB(120, 10, 0);//红橙色
                Update_WS2812B_Color_To_Aliyun(1);
                //Serial.println("0 color");
                break;
            }

            case 1:
            {
                WS2812B_SetGRB(70, 150, 255);//蓝白色
                Update_WS2812B_Color_To_Aliyun(2);
                //Serial.println("1 color");
                break;
            }
            
            case 2:
            {
                WS2812B_SetGRB(80, 40, 240);//紫白色
                Update_WS2812B_Color_To_Aliyun(3);
                //Serial.println("2 color");
                break;
            }

            case 3:
            {
                WS2812B_SetGRB(0, 100, 50);//青色
                Update_WS2812B_Color_To_Aliyun(4);
                //Serial.println("3 color");
                break;
            }

            case 4:
            {
                WS2812B_SetGRB(90, 0, 30);//玫瑰色
                Update_WS2812B_Color_To_Aliyun(5);
                //Serial.println("4 color");
                break;
            }

            case 5:
            {
                WS2812B_SetGRB(30, 60, 100);
                Update_WS2812B_Color_To_Aliyun(6);
                //Serial.println("5 color");
                break;
            }

            case 6:
            {
                WS2812B_SetGRB(160, 80, 0);//黄色
                Update_WS2812B_Color_To_Aliyun(7);
                //Serial.println("6 color");
                break;
            }

            case 7:
            {
                nightled_flag = true;
                Update_WS2812B_Color_To_Aliyun(8);
                break;
            }

            default:
            {
                led_ctrl_idx = 0;
                break;
            }

            }
        }
        
        break;
    }
    
    case MENU_EVENT_LONG_BUTTON1:
    {
        if(machine_rest_flag != MACHINE_CLOSE)
        {
            nightled_flag = false;//关呼吸灯

            if(led_temp == 0)
            {
                //WS2812B_SetGRB(0, 0, 0);
                WS2812B_Close();
                Update_WS2812B_Color_To_Aliyun(0);
                led_temp = 1;
            }
            else
            {
                led_temp = 0;
                switch(led_ctrl_idx)
                {
                case 0:
                {
                    WS2812B_SetGRB(120, 10, 0);
                    Update_WS2812B_Color_To_Aliyun(1);
                    //Serial.println("0 color");
                    break;
                }

                case 1:
                {
                    WS2812B_SetGRB(70, 150, 255);
                    Update_WS2812B_Color_To_Aliyun(2);
                    //Serial.println("1 color");
                    break;
                }

                case 2:
                {
                    WS2812B_SetGRB(80, 40, 240);
                    Update_WS2812B_Color_To_Aliyun(3);
                    //Serial.println("2 color");
                    break;
                }

                case 3:
                {
                    WS2812B_SetGRB(0, 100, 50);
                    Update_WS2812B_Color_To_Aliyun(4);
                    //Serial.println("3 color");
                    break;
                }

                case 4:
                {
                    WS2812B_SetGRB(90, 0, 30);
                    Update_WS2812B_Color_To_Aliyun(5);
                    //Serial.println("4 color");
                    break;
                }

                case 5:
                {
                    WS2812B_SetGRB(30, 60, 100);
                    Update_WS2812B_Color_To_Aliyun(6);
                    //Serial.println("5 color");
                    break;
                }

                case 6:
                {
                    WS2812B_SetGRB(160, 80, 0);
                    Update_WS2812B_Color_To_Aliyun(7);
                    //Serial.println("6 color");
                    break;
                }

                case 7:
                {
                    WS2812B_Close();
                    Update_WS2812B_Color_To_Aliyun(0);
                    break;
                }

                default:
                {
                    break;
                }

                }
            }
        }
        break;
    }

    case MENU_EVENT_TWO_BUTTON1:
    {
        switch(led_switch)
        {
        case 0:
        {
            led_switch = 1;
            Led_Open();
            Update_LED_Switch_To_Aliyun(1);
            break;
        }
        
        case 1:
        {
            led_switch = 0;
            Led_Close();
            Update_LED_Switch_To_Aliyun(0);
            break;
        }
        }
        break;
    }

    case MENU_EVENT_BUTTON2:
    {
        if(machine_rest_flag != MACHINE_CLOSE)
        {
            if(water_switch == 0)
            {
                Atomizer_Open();
                water_switch = 1;
            }
            else
            {
                Atomizer_Close();
                water_switch = 0;
            }
            Update_Humidified_To_Aliyun(water_switch);
        }
        break;
    }

    case MENU_EVENT_LONG_BUTTON2:
    {
        if(machine_rest_flag == MACHINE_CLOSE)
        {
            Serial.println("machine restart");
            ESP.restart();//重启
        }
        else if(machine_rest_flag == MACHINE_OPEN)
        {
            machine_rest_flag = MACHINE_CLOSE;
            Serial.println("machine off");
            nightled_flag = false;//关闭呼吸灯
            Atomizer_Close();//关闭雾化器
            WS2812B_Close();//关闭灯带
            Update_WS2812B_Color_To_Aliyun(0);//更新WS2812B的颜色状态
            Update_Humidified_To_Aliyun(0);//更新雾化器为关闭
            Update_WS2812B_Brightness_To_Aliyun(0);//更新WS2812B的亮度
            Serial1.end();
            Serial2.end();
            WiFi.disconnect();//断开WiFi
        }
        break;
    }
    
    case MENU_EVENT_TWO_BUTTON3://open smartconfig
    {
        if(machine_rest_flag != MACHINE_CLOSE)
        {
            if(Get_Wifi_Mode() == 0)
            {
                WiFiMode_Set(0);//进入smartconfig模式
            }
            else
            {
                WiFiMode_Set(1);//退出smartconfig模式
            }
        }
        break;
    }
    
    case MENU_EVENT_NULL:
    {
        break;
    }

    default:
    {
        break;
    }
    }
}